import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class InputLauncher {
	public static String sourceIP = "127.0.0.1";
	public static String[] destIP = {"127","0","0","1"};
   /**
    * @param args
    */
   public static void main(String[] args) throws Exception {	   
	  
	  String[] info = getInfo();	  
	    
	  if(info[1].equals("1")){
		  String[][] config = getClientConfigurations(info);
	      SourceThread source1 = new SourceThread(config[0]);
	      //if the task is greater than 1.1 then we know there are > 1 senders/receivers
	      if(!info[0].equals("1.1")){
	          SourceThread source2 = new SourceThread(config[1]);
	          System.out.println("2 starting");
	          source2.start();
	      }
	      source1.start();
	  } else if(info[1].equals("2")){
		  String[][] config = getServDestConfigurations(info);		  
	      ServerThread serv = new ServerThread(config[0]);
	      DestThread dest1 = new DestThread(config[1]);	      
	      //if the task is greater than 1.1 then we know there are > 1 senders/receivers
	      if(!info[0].equals("1.1")){
	    	  DestThread dest2 = new DestThread(config[2]);	    	  
	    	  dest2.start();
	      }
	      serv.start();
	      dest1.start();
	  } else{
		  System.out.println("wrong input");
		  System.out.println(info[1]);
	  }

   }

   private static String[][] getServDestConfigurations(String[] args){
	   //args = {task#, machine#, rate}
	   //task 1.1
	   if(args[0].equals("1.1")){
		   String[] server = {"9101","9105","64","0","1"};
		   String[] dest1 = {"9101","9109","false","127.0.0.1"};	   
		   String[][] configs = {server,dest1};
		   return configs;
	   }
	   else	if(args[0].equals("2.1")){
		   String[] server = {"9101","9105","64","0","1"};
		   String[] dest1 = {"9101","9109","false",InputLauncher.sourceIP};	   
		   String[] dest2 = {"9105","9113","false",InputLauncher.sourceIP};	   		   
		   String[][] configs = {server,dest1,dest2};
		   return configs;		   
	   } else if(args[0].equals("3.1")){
		   String[] server = {"9101","9105","32","32","0.6"};
		   String[] dest1 = {"9101","9109","false",InputLauncher.sourceIP};	   
		   String[] dest2 = {"9105","9113","false",InputLauncher.sourceIP};	   		   
		   String[][] configs = {server,dest1,dest2};
		   return configs;		   		   
	   } else if(args[0].equals("3.3")){
		   String[] server = {"9101","9105","32","32","0.6"};
		   String[] dest1 = {"9101","9109","false",InputLauncher.sourceIP};	   
		   String[] dest2 = {"9105","9113","true",InputLauncher.sourceIP};	   		   
		   String[][] configs = {server,dest1,dest2};
		   return configs;		   		   
	   } else if(args[0].equals("4.1")){
		   String[] server = {"9101","9105","32","32","0.6"};
		   String[] dest1 = {"9101","9109","false",InputLauncher.sourceIP, "flow1.gif"};	   
		   String[] dest2 = {"9105","9113","false",InputLauncher.sourceIP, "flow2.gif"};	   		   
		   String[][] configs = {server,dest1,dest2};
		   return configs;		   		   		   
	   } else if(args[0].equals("4.2")){
		   String[] server = {"9101","9105","32","32","0.6"};
		   String[] dest1 = {"9101","9109","false",InputLauncher.sourceIP, "flow1.gif"};	   
		   String[] dest2 = {"9105","9113","true",InputLauncher.sourceIP, "flow2.gif"};	   		      
		   String[][] configs = {server,dest1,dest2};
		   return configs;		   		   		   		   
	   } else {
		   System.out.println("woops, the task number was not recognized...");
		   String[] things = {"stuff"};
		   String[][] stuff = {things};
		   return stuff;
	   }
	   //task 2.1 (and task 2.2, they are the same)
	   //task 3.1 (and task 3.2, they are the same)
	   //task 3.3
	   //task 4.1
   }
   
   private static String[][] getClientConfigurations(String[] args){
	   //args = {task#, machine#, rate}
	   if(args[0].equals("1.1")){
		   String[] client1 = {InputLauncher.destIP[0],InputLauncher.destIP[1],InputLauncher.destIP[2],InputLauncher.destIP[3],
				   "9101",args[3],"10000","false", "9109","false"};
		   String[][] configs = {client1};
		   return configs;		   
	   } else if(args[0].equals("2.1")){
		   String client1[] = {InputLauncher.destIP[0],InputLauncher.destIP[1],InputLauncher.destIP[2],InputLauncher.destIP[3],
		           "9101",args[2],"10000","false", "9109","false"};
		   String client2[] = {InputLauncher.destIP[0],InputLauncher.destIP[1],InputLauncher.destIP[2],InputLauncher.destIP[3],
				   "9105",args[3],"10000","false", "9113","false"};
		   String[][] configs = {client1, client2};
		   return configs;		   		   	
	   } else if(args[0].equals("3.1")){
		   String client1[] = {InputLauncher.destIP[0],InputLauncher.destIP[1],InputLauncher.destIP[2],InputLauncher.destIP[3],
				   "9101",args[2],"10000","false", "9109","false"};
		   String client2[] = {InputLauncher.destIP[0],InputLauncher.destIP[1],InputLauncher.destIP[2],InputLauncher.destIP[3],
				   "9105",args[3],"10000","false", "9113","false"};
		   String[][] configs = {client1, client2};
		   return configs;		   		   			   
	   } else if(args[0].equals("3.3")){
		   String client1[] = {InputLauncher.destIP[0],InputLauncher.destIP[1],InputLauncher.destIP[2],InputLauncher.destIP[3],
				   "9101",args[2],"10000","false", "9109","false"};
		   String client2[] = {InputLauncher.destIP[0],InputLauncher.destIP[1],InputLauncher.destIP[2],InputLauncher.destIP[3],
				   "9105",args[3],"10000","false", "9113","true"};
		   String[][] configs = {client1, client2};
		   return configs;		   		   			   
	   } else if(args[0].equals("4.1")){
		   String client1[] = {InputLauncher.destIP[0],InputLauncher.destIP[1],InputLauncher.destIP[2],InputLauncher.destIP[3],
				   "9101",args[2],"10000","false", "9109","false","test.gif"};
		   String client2[] = {InputLauncher.destIP[0],InputLauncher.destIP[1],InputLauncher.destIP[2],InputLauncher.destIP[3],
				   "9105",args[3],"10000","false", "9113","false","test.gif"};
		   String[][] configs = {client1, client2};
		   return configs;		   		   			   		   
	   } else if(args[0].equals("4.2")){
		   String client1[] = {InputLauncher.destIP[0],InputLauncher.destIP[1],InputLauncher.destIP[2],InputLauncher.destIP[3],
				   "9101",args[2],"10000","false", "9109","false","test.gif"};
		   String client2[] = {InputLauncher.destIP[0],InputLauncher.destIP[1],InputLauncher.destIP[2],InputLauncher.destIP[3],
				   "9105",args[3],"10000","false", "9113","true","test.gif"};
		   String[][] configs = {client1, client2};
		   return configs;		   		   			   		   
	   } else {
		   System.out.println("woops, the task number was not recognized...");
		   System.out.println("woops, the task number was not recognized...");
		   String[] things = {"stuff"};
		   String[][] stuff = {things};
		   return stuff;
	   }
	   
	   
   }
   
   private static class ServerThread extends Thread {
      private String args[];
      
      public ServerThread(String in_args[]) {
         args = in_args;
      }
      
      public void run() {
         try {
            UDPServer.main(args);
         } catch(Exception e) {
            e.printStackTrace();
         }
      }
   }
   
   private static class SourceThread extends Thread {
      private String args[];
      
      public SourceThread(String in_args[]) {
         args = in_args;
      }
      
      public void run() {
         try {
            UDPClient.main(args);
         } catch(Exception e) {
            e.printStackTrace();
         }
      }
   }

   
   public static String[] getInfo(){
	   //task#, machine#, rate
	   System.out.print("Please enter <task#,machine#,rate> : ");
	   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	   String input = "";
	   try{
		   input = br.readLine();
	   } catch(IOException e){
		   System.out.println("Error reading input...");
		   System.exit(1);
	   }
	   String[] config = input.split(",");
	   int temp = Integer.parseInt(config[2]);
	   temp = temp*2;
	   String[] newThing = {config[0],config[1], String.valueOf(temp), config[2]};
	   config = newThing;
	   for(int i =0; i< config.length; i++){
		   System.out.println(config[i]);		   
	   }	   
	   return newThing;
   }   
   
   private static class DestThread extends Thread {
      private String args[];
      
      public DestThread(String in_args[]) {
         args = in_args;
      }
      
      public void run() {
         try {
            UDPDestination.main(args);
         } catch(Exception e) {
            e.printStackTrace();
         }
      }
   }

}
